<template>
  <div class="home">
     <header>商品列表</header>
     <ul>
       <li v-for="(item,index) in list" :class="{active:checkIndex==index}" :key="index" @click="checkIndex=index">{{item.name}}</li>
     </ul>
     <ol>
       <li v-for="(item,index) in list[checkIndex]?.child" :key="index">
         <img src="../assets/logo.png" alt="">
         <section>
           <h5>{{item.name}}</h5>
           <p style="color:red">￥{{item.price}}</p>
           <div>
             <span>{{item.payNum}}人付款</span>
             <button @click="jump(item)">+</button>
           </div>
         </section>
       </li>
     </ol>
  </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import axios from 'axios';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
export default {
  setup() {

    const store = useStore();
    const { push } = useRouter();
    const data = reactive({
       list: [], // 商品的数据
       checkIndex: 0, // 选中的索引
    })
    axios.get('data.json').then(res => {
      data.list = res.data;
    })
    
    const jump = (val) =>{
       store.commit('saveCart',val);
       push('/about');
    }
    return{
      ...toRefs(data),
      jump
    }
  },
};
</script>
<style lang="scss" scoped>
.home{
  height: 100%;
  background: #e9e9e9;
  header{
    line-height: 45px;
    text-align: center;
    background: #fff;
  }
  ul{
    display: flex;
    height: 50px;
    background: #574483;
    color: #fff;
    align-items: center;
    li{
      width: 20%;
      text-align: center;
      line-height: 50px;
    }
    .active{
      background: #8d58f6;
    }
  }
}
ol>li{
  display: flex;
  background: #fff;
  align-items: center;
  padding: 10px;
  margin-bottom: 10px;
  img{
    width: 80px;
  }
  section{
    flex: 1;
    margin-left: 10px;
    button{
      float: right;
      border: none;
      padding: 3px 10px;
      border-radius: 8px;
      background: #8d58f6;
      color: #fff;
    }
  }
}
</style>